[bits 32]

extern _IDTTable
extern _SyscallTable

section .data
global _IntEntryTable
_IntEntryTable:

%macro VECTOR 1
section .text
Intr%1Entry:
    sub esp, 4
    cld
    push ds
    push es
    push fs
    push gs
    pushad
    push dword %1
    call [_IDTTable + %1 * 4]
    jmp IntrExit
section .data
    dd Intr%1Entry
%endmacro

%macro VECTORE 1
section .text
Intr%1Entry:
    cld
    push ds
    push es
    push fs
    push gs
    pushad
    push dword %1
    call [_IDTTable + %1 * 4]
    jmp IntrExit
section .data
    dd Intr%1Entry
%endmacro

section .text
global IntrExit
IntrExit:
   add esp,4
   popad
   pop gs
   pop fs
   pop es
   pop ds
   add esp,4
   iretd

VECTOR 0x00
VECTOR 0x01
VECTOR 0x02
VECTOR 0x03
VECTOR 0x04
VECTOR 0x05
VECTOR 0x06
VECTOR 0x07
VECTOR 0x08
VECTOR 0x09
VECTORE 0x0a
VECTORE 0x0b
VECTOR 0x0c
VECTORE 0x0d
VECTORE 0x0e
VECTOR 0x0f
VECTOR 0x10
VECTORE 0x11
VECTOR 0x12
VECTOR 0x13
VECTOR 0x14
VECTOR 0x15
VECTOR 0x16
VECTOR 0x17
VECTORE 0x18
VECTOR 0x19
VECTORE 0x1a
VECTORE 0x1b
VECTOR 0x1c
VECTORE 0x1d
VECTORE 0x1e
VECTOR 0x1f
VECTOR 0x20    ;clock interrupt 
VECTOR 0x21	;keyboard interrupt
VECTOR 0x22	;
VECTOR 0x23	;com2 entry
VECTOR 0x24	;com1 entry
VECTOR 0x25	;parallel port2 entry
VECTOR 0x26	;floppy entry
VECTOR 0x27	;parallel port1 entry
VECTOR 0x28	;real clock interrupt
VECTOR 0x29	;redirected
VECTOR 0x2a	;reserved
VECTOR 0x2b	;reserved
VECTOR 0x2c	;ps/2 mouse
VECTOR 0x2d	;fpu exception
VECTOR 0x2e	;harddisk
VECTOR 0x2f	;reserved
VECTOR 0x30
VECTOR 0x31
VECTOR 0x32
VECTOR 0x33
VECTOR 0x34
VECTOR 0x35
VECTOR 0x36
VECTOR 0x37
VECTOR 0x38
VECTOR 0x39
VECTOR 0x3a
VECTOR 0x3b
VECTOR 0x3c
VECTOR 0x3d
VECTOR 0x3e
VECTOR 0x3f
VECTOR 0x40
VECTOR 0x41
VECTOR 0x42
VECTOR 0x43
VECTOR 0x44
VECTOR 0x45
VECTOR 0x46
VECTOR 0x47
VECTOR 0x48
VECTOR 0x49
VECTOR 0x4a
VECTOR 0x4b
VECTOR 0x4c
VECTOR 0x4d
VECTOR 0x4e
VECTOR 0x4f
VECTOR 0x50
VECTOR 0x51
VECTOR 0x52
VECTOR 0x53
VECTOR 0x54
VECTOR 0x55
VECTOR 0x56
VECTOR 0x57
VECTOR 0x58
VECTOR 0x59
VECTOR 0x5a
VECTOR 0x5b
VECTOR 0x5c
VECTOR 0x5d
VECTOR 0x5e
VECTOR 0x5f
VECTOR 0x60
VECTOR 0x61
VECTOR 0x62
VECTOR 0x63
VECTOR 0x64
VECTOR 0x65
VECTOR 0x66
VECTOR 0x67
VECTOR 0x68
VECTOR 0x69
VECTOR 0x6a
VECTOR 0x6b
VECTOR 0x6c
VECTOR 0x6d
VECTOR 0x6e
VECTOR 0x6f
VECTOR 0x70
VECTOR 0x71
VECTOR 0x72
VECTOR 0x73
VECTOR 0x74
VECTOR 0x75
VECTOR 0x76
VECTOR 0x77
VECTOR 0x78
VECTOR 0x79
VECTOR 0x7a
VECTOR 0x7b
VECTOR 0x7c
VECTOR 0x7d
VECTOR 0x7e
VECTOR 0x7f

;;;;;;;;;;;;;;;;   0x80 interrupt   ;;;;;;;;;;;;;;;;
section .text
global _SyscallHandler
_SyscallHandler:
;save context
   push dword 0			; format stack(to meet the intr_exit stack require)
   push ds
   push es
   push fs
   push gs
   pushad			    ; PUSHAD: EAX,ECX,EDX,EBX,ESP,EBP,ESI,EDI 
   push dword 0x80		; format stack(to meet the intr_exit stack require)
   cmp eax, 0
   jae IntrExit
;push argc of syscall
   push edx			    ; the 3rd argc
   push ecx			    ; the 2th argc
   push ebx			    ; the 1st argc
;call handler of sub_function
   call [_SyscallTable + eax*4]	    ; index in table by eax
   add esp, 12			    ; balance stack to skip three argc
;save return val(eax) to eax_stack_memory
   mov [esp + 8*4], eax	
   jmp IntrExit		    ; intr_exit to recover context
